package npcSystem.mainCharSystem;

import java.awt.Graphics;
import java.awt.image.BufferedImage;

import tools.Constants;
import tools.Rect;

public class Weapon {

	private double power;
	private BufferedImage sprite;
	private Rect[][] clips;
	
	public Weapon(double pwr, BufferedImage img) {
		power = pwr;
		sprite = img;
		
		clips = new Rect[img.getWidth()/Constants.MAIN_CHAR_SPRITE_WIDTH][8];
		setClips();
	}
	
	public void draw(Graphics g, int curFrame, int curDir, int type, double x, double y, Rect cam)
	{
		type = type-1;
		switch(curDir)
		{
			case 1:
				g.drawImage(sprite.getSubimage((int)clips[curFrame][type*4].x, 
						(int)clips[curFrame][type*4].y, 
						(int)clips[curFrame][type*4].w, (int)clips[curFrame][type*4].h), 
						(int)(x - Constants.WEAPON_SPRITE_WIDTH/2 - cam.x - Constants.DELTA_X), 
						(int)(y - (Constants.WEAPON_SPRITE_HEIGHT*type/2) - cam.y), 
						null);
				break;
			case 2:
				g.drawImage(sprite.getSubimage((int)clips[curFrame][type*4+1].x, 
						(int)clips[curFrame][type*4+1].y, 
						(int)clips[curFrame][type*4+1].w, (int)clips[curFrame][type*4+1].h), 
						(int)(x - cam.x - Constants.DELTA_X), 
						(int)(y - (Constants.WEAPON_SPRITE_HEIGHT*type/2) - cam.y), 
						null);
				break;
			case 3:
				g.drawImage(sprite.getSubimage((int)clips[curFrame][type*4+2].x, 
						(int)clips[curFrame][type*4+2].y, 
						(int)clips[curFrame][type*4+2].w, (int)clips[curFrame][type*4+2].h), 
						(int)(x - cam.x - Constants.DELTA_X), 
						(int)(y - Constants.WEAPON_SPRITE_HEIGHT/2 - cam.y), 
						null);
				break;
			case 4:
				g.drawImage(sprite.getSubimage((int)clips[curFrame][type*4+3].x, 
						(int)clips[curFrame][type*4+3].y, 
						(int)clips[curFrame][type*4+3].w, (int)clips[curFrame][type*4+3].h), 
						(int)(x - cam.x - Constants.DELTA_X - Constants.WEAPON_SPRITE_WIDTH/2), 
						(int)(y - cam.y), 
						null);
				break;
		}
	}
	
	public double getPower()
	{
		return power;
	}
	
	public void setClips() {
		for(int i = 0; i < clips.length; i++)
		{
			for(int j = 0; j < 8; j++)
			{
				clips[i][j] = new Rect(i*Constants.WEAPON_SPRITE_WIDTH, 
						j*Constants.WEAPON_SPRITE_HEIGHT, 
						Constants.WEAPON_SPRITE_WIDTH, 
						Constants.WEAPON_SPRITE_HEIGHT);
			}
		}
	}
}
